In [7]:
import numpy as np
from sklearn import preprocessing
In [8]:
# Example labels
labels = np.array([1,5,3,2,1,4,2,1,3])
print(labels)
In [9]:
# Create the encoder
lb = preprocessing.LabelBinarizer()
In [10]:
# Here the encoder finds the classes and assigns one-hot vectors
lb.fit(labels)
Out[10]:
In [11]:
# And finally, transform the labels into one-hot encoded vectors
lb.transform(labels)
Out[11]: